<?php 
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller{
     public function yzm(){
     	ob_clean();
    	$verify = new \Think\Verify();
    	$verify->imageW = 135;
    	$verify->imageH = 40;
    	$verify->fontSize = 20;
    	$verify->length = 4;
    	$verify->useNoise = false;
    	$verify->entry();		
    }

	public function checkYzm($yzm){
		$verify = new \Think\Verify();
		if($verify->check($yzm)){
			return true;
		}else{
			echo '验证码错误';
			exit;
		}
	}

	public function login(){
		if(!IS_POST){
			$this->display();
		//var_dump($_POST);exit();
		}else{
			if($this->checkYzm(I('post.yzm'))){
				$user = D('User')->where('username='.'"'.I('post.username').'"')->find();
				if(!md5(I('post.password').$user['salt']) === $user['password']){
					echo '密码或用户名不对';
				}else{
					cookie('username',$user['username']);
					cookie('coode',md5($user['username'].C('salt')));
					$this->redirect('/');
				}
			}else{
				echo '验证码不对';
			}
		}
	}

	public function logout(){
		cookie('username',null);
		cookie('coode',null);
		$this->redirect('/');
	}

	public function msg(){
		$this->display();
	}

	public function reg(){
		if(!IS_POST){
			$this->display();
		}else{
			$userModel = D('User');
			if(!$userModel->create()){
				echo $userModel->getError();
			}else{
				$yan = $this->yan();
				$userModel->password = md5($userModel->password.$yan);
				$userModel->salt = $yan;
				if($userModel->add()){
					$this->redirect('Home/User/login');
				}
			}
		}
	}
	public function yan(){
		return mt_rand(10000,99999);
	}
}